//pkg/errors 示例

package main

import (
	"fmt"
	xerrors "github.com/pkg/errors"
	"io/ioutil"
	"os"
	"path/filepath"
)

func ReadFile(path string) ([]byte, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, xerrors.Wrap(err, "open failed")
	}
	defer f.Close()

	fd, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, xerrors.Wrap(err, "read failed")
	}
	return fd, err
}

func ReadConfig() ([]byte, error) {
	home := os.Getenv("HOME")
	config, err := ReadFile(filepath.Join(home, ".settings.xml"))
	return config, xerrors.WithMessagef(err, "could not read config")
}

func main() {
	_, err := ReadConfig()
	if err != nil {
		//原始信息
		fmt.Printf("original error : %T %v\n", xerrors.Cause(err), xerrors.Cause(err))
		//栈信息
		fmt.Printf("stack trace : \n %+v\n", err)
		os.Exit(0)
	}
}
